#include "user.h"

//key0-PF9,key1-PF8,key2-PF7,key3-PF6

void key_exti_init()
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
	
	GPIO_InitTypeDef gpioStruct;
	gpioStruct.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6;
	gpioStruct.GPIO_Mode=GPIO_Mode_IN;
	gpioStruct.GPIO_PuPd=GPIO_PuPd_UP;
	gpioStruct.GPIO_Speed=GPIO_Low_Speed;
	GPIO_Init(GPIOF,&gpioStruct);
	
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOF,EXTI_PinSource9);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOF,EXTI_PinSource8);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOF,EXTI_PinSource7);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOF,EXTI_PinSource6);
	
	EXTI_InitTypeDef extiStruct;
	extiStruct.EXTI_Line=EXTI_Line9|EXTI_Line8|EXTI_Line7|EXTI_Line6;
	extiStruct.EXTI_LineCmd=ENABLE;
	extiStruct.EXTI_Mode=EXTI_Mode_Interrupt;
	extiStruct.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_Init(&extiStruct);
	
	
	NVIC_InitTypeDef nvicStruct;
	nvicStruct.NVIC_IRQChannel=EXTI9_5_IRQn;
	nvicStruct.NVIC_IRQChannelCmd=ENABLE;
	nvicStruct.NVIC_IRQChannelPreemptionPriority=1;
	nvicStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&nvicStruct);
}

void EXTI9_5_IRQHandler()
{
	if(EXTI_GetITStatus(EXTI_Line9)!=RESET)
	{
		LCD_DisplayOn();
		timer2_init(8400,10000);
		EXTI_ClearITPendingBit(EXTI_Line9);
	}
	else if(EXTI_GetITStatus(EXTI_Line8)!=RESET)
	{
		LCD_DisplayOff();
		TIM2_close();
		EXTI_ClearITPendingBit(EXTI_Line8);
	}
	else if(EXTI_GetITStatus(EXTI_Line7)!=RESET)
	{
		buzzer_turn_on();
		EXTI_ClearITPendingBit(EXTI_Line7);
	}
	else if(EXTI_GetITStatus(EXTI_Line6)!=RESET)
	{
		buzzer_turn_off();
		EXTI_ClearITPendingBit(EXTI_Line6);
	}
}

